package homework;

public class p1653 implements Runnable{

    private int allPages=45;

    public p1653(){
        ReadThread readThread1=new ReadThread(this,"first");
        ReadThread readThread2=new ReadThread(this,"second");
        readThread1.start();
        readThread2.start();
    }

    public static void main(String[] args){
        new p1653();
    }

    @Override
    public void run()   {
        ReadThread thread=(ReadThread)Thread.currentThread();
        if(!thread.getName().equalsIgnoreCase("first")){
            synchronized (this) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }

        while (true){
            System.out.println(thread.getName()+" student is reading the "+thread.increasePages()+" page");

            if (thread.getPages()%5==0){
                synchronized (this){
                    System.out.println("------------Alter Student to Reading--------");
                    notify();

                    if (thread.getPages()==this.allPages) {
                        break;
                    }

                    try {
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }


                }
                }
            }
        }
    }




class ReadThread extends Thread{
    private int pages=0;

    public ReadThread(Runnable r,String name){
        super(r,name);
    }

    public int getPages() {
        return pages;
    }

    public int increasePages(){
        return ++pages;
    }


}